package com.ispeng.databaselist;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase readableDatabase;
    private String userName;
    private String password;
    private EditText name;
    private EditText pass;
    private ListView listView;
    private List<Person> persons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.et_username);
        pass = (EditText) findViewById(R.id.et_password);
        listView = (ListView) findViewById(R.id.lv_data);
        DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext(), "manager.db", null, 1);
        readableDatabase = databaseHelper.getReadableDatabase();
        showAll();
    }

    private Boolean chlickNamePass(EditText name, EditText pass) {
        if (name != null && pass != null) {
            userName = name.getText().toString().trim();
            password = pass.getText().toString().trim();
            return true;
        } else {
            Toast.makeText(this, "用户或密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    //添加
    public void add(View view) {
        Boolean is = chlickNamePass(name, pass);
        if (is) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("user_name", userName);
            contentValues.put("password", password);
            long user = readableDatabase.insert("user", null, contentValues);
            showAll();
            if (0 < user)
                Toast.makeText(MainActivity.this, "添加用户成功", Toast.LENGTH_SHORT).show();
        }

    }


    //修改
    public void update(View view) {
        Boolean is = chlickNamePass(name, pass);
        if (is) {
            ContentValues values = new ContentValues();
            values.put("password", password);
            //修改条件
            String whereClause = "user_name=?";
            String[] whereArgs = {userName};
            //返回受影响行数
            int user = readableDatabase.update("user", values, whereClause, whereArgs);
            showAll();
            if (0 < user)
                Toast.makeText(MainActivity.this, "修改用户成功", Toast.LENGTH_SHORT).show();
        }
    }


    //删除
    public void delete(View view) {
        Boolean is = chlickNamePass(name, pass);
        if (is) {
            String whereClause = "user_name=?";
            String[] whereArgs = {userName};
            int user = readableDatabase.delete("user", whereClause, whereArgs);
            showAll();
            if (0 < user)
                Toast.makeText(MainActivity.this, "删除用户成功", Toast.LENGTH_SHORT).show();
        }
    }


    //查询
    public void query(View view) {
        showAll();
    }

    private void showAll() {
        Cursor user = readableDatabase.query("user", null, null, null, null, null, null);
        if (0 < user.getCount()) {
            persons = new ArrayList<Person>();
            while (user.moveToNext()) {
                Person person = new Person();
                person.setId(user.getInt(0));
                person.setUserName(user.getString(1));
                person.setPassword(user.getString(2));
                System.out.println(person);
                persons.add(person);
            }
            listView.setAdapter(new MyAdapter());
        } else {
            Toast.makeText(MainActivity.this, "未查询到数据", Toast.LENGTH_SHORT).show();
        }
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return persons.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = View.inflate(getApplicationContext(), R.layout.item, null);
            } else {
                view = convertView;
            }
            TextView name = (TextView) view.findViewById(R.id.tv_name);
            TextView pass = (TextView) view.findViewById(R.id.tv_pass);

            Person person = persons.get(position);
            name.setText(person.getUserName());
            pass.setText(person.getPassword());

            return view;
        }
    }

}


